package base;


import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ma.R;
import com.yanzhenjie.permission.AndPermission;

import utils.ActivityStack;
import utils.JsonUtil;


public abstract class BaseActivity extends AppCompatActivity  {


    private ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityStack.get().add(this);
        ActionBar mActionBar = getSupportActionBar();
        if (mActionBar != null) {
            mActionBar.setDisplayHomeAsUpEnabled(true);
            mActionBar.setElevation(0);
        }



    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public Bundle getBundle() {
        return getIntent().getExtras();
    }


    public void log(Object s) {
        Log.d("zjt", s != null ? s.toString() : "Null Msg");

    }

    public void logJson(Object s) {
        Log.d("zjt", s != null ? JsonUtil.toJson(s) : "Null Msg");

    }


    public void startActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);


    }

    public void toast(String text) {
        if (TextUtils.isEmpty(text))
            return;
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();

    }


    public void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("请稍后...");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }

        progressDialog.show();
    }

    public void showProgressDialog(String msg) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setCancelable(false);
            progressDialog.setMessage(msg);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//            progressDialog.setIndeterminate(true);
//            progressDialog.setProgressNumberFormat(null);
//            progressDialog.setProgressPercentFormat(null);
        }

        progressDialog.show();
    }


    public void closeProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    public void showDialog(String msg) {


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息");
        builder.setMessage(msg);

        builder.setPositiveButton("确定", (dialog, which) -> {
            dialog.dismiss();
        });
        builder.show();


    }

    public void showDialog(String msg, DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息");
        builder.setMessage(msg);
        builder.setNegativeButton(android.R.string.cancel, (dialog, which) -> {
            dialog.dismiss();
        });
        builder.setPositiveButton(android.R.string.ok, onClickListener);
        builder.show();


    }

    public void showCustomDialog(String titel, String msg, View.OnClickListener yesclick, View.OnClickListener noClick) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.custome_dialog, null);


        TextView  mTitle = (TextView) view.findViewById(R.id.title);
        ImageView   mClose = (ImageView) view. findViewById(R.id.close);
        TextView  mMsg = (TextView)  view.findViewById(R.id.msg);
        Button mYes = (Button) view. findViewById(R.id.yes);

        Button  mNo = (Button)  view.findViewById(R.id.no);


        mTitle.setText(titel);
        mMsg.setText(msg);
        builder.setView(view);
        AlertDialog  dialog=    builder.create();
        mClose.setOnClickListener(view1 -> {
            dialog.dismiss();
        });
        mYes.setOnClickListener(view1 -> {
            yesclick.onClick(view1);
            dialog.dismiss();

        });
        mNo.setOnClickListener(view1 -> {
            noClick.onClick(view1);
            dialog.dismiss();
        });

        dialog.show();


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        ActivityStack.get().removeActivity(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        // 只需要调用这一句，剩下的AndPermission自动完成。
        AndPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }





}
